/*
 * Copyright 2020 tu.cn All right reserved. This software is the
 * confidential and proprietary information of tu.cn ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Tu.cn
 */
package cn.zjitc.dao.impl;

import cn.zjitc.dao.ProductDao;
import cn.zjitc.domian.Product;
import cn.zjitc.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/**
 * @author dell
 * @date 2020/7/3 8:42
 */
public class ProductDaoImpl implements ProductDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public List<Product> findAll() {
        String sql  =" select * from product";
        return template.query(sql,new BeanPropertyRowMapper<>(Product.class));
    }

    @Override
    public void addProduct(Product product) {
        String sql  =" insert into product values (null,?,?,?)";
        template.update(sql,product.getName(),product.getPrice(),product.getPlace());
    }

    @Override
    public Product findByName(String name) {
        Product product = null;
        try {
            String sql = "select * from product where name = ?";
             product = template.queryForObject(sql, new BeanPropertyRowMapper<>(Product.class), name);
        }catch (Exception e){

        }
        return product;
    }

    @Override
    public void delProduct(int id) {
        String sql = " delete from product where id = ?";
        template.update(sql,id);
    }

    @Override
    public Product findById(int id) {
        String sql = "select * from product where id = ?";
        Product product = template.queryForObject(sql, new BeanPropertyRowMapper<>(Product.class), id);
        return product;
    }

    @Override
    public void update(Product product) {
        String sql = "update product set name = ? , price = ?, place = ? where id  =? ";
        template.update(sql,product.getName(),
                product.getPrice(),product.getPlace(),product.getId());
    }
}
